﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using iLangl.Model;

namespace iLangl.Customer.DAL.XMLStorage
{
	public class ContentDAL : BaseStorageDAL, IContentDAL
	{
		#region IContentDAL Members


		public ContentDescription ReadContentDescription(string contentId)
		{
			ContentEntity result = ReadContent(contentId);
			//Please do not repeat this mockup loginc
			result.Content = string.Empty;
			return result;
		}

		public ContentEntity ReadContent(string contentId)
		{
			dtoXmlStorage.ContentRow contentrow = Storage.Content.FindById(contentId);
			CustomerContentEntity content = new CustomerContentEntity();
			content.Id = contentrow.Id;

			content.Language = new Language(contentrow.LanguageCode, String.Empty);
			content.Content = contentrow.Content;
			content.Name = contentrow.Name;
			content.Type = contentrow.Type;
			content.Version = contentrow.Version;

			if (!contentrow.IsCustomerIdentityNull())
				content.CustomerIdentity = contentrow.CustomerIdentity;

			if (!contentrow.IsTPIdentityNull())
				content.TPIdentity = contentrow.TPIdentity;

			return content;
		}

		public void CreateContent(ContentEntity content, System.Data.IDbTransaction transaction)
		{
			dtoXmlStorage.ContentRow contentRow = Storage.Content.FindById(content.Id);

			bool isNew = (contentRow == null);

			if (isNew)
			{
				contentRow = Storage.Content.NewContentRow();
			}

			contentRow.Content = content.Content;
			contentRow.Id = content.Id;
			if(content.Language != null)
			contentRow.LanguageCode = content.Language.LanguageId;

			contentRow.Name = content.Name;
			if(content.Project != null)
				contentRow.ProjectId = content.Project.Id;

			contentRow.Type = content.Type;
			contentRow.Version = content.Version;

			contentRow.CustomerIdentity = content.CustomerIdentity;
			contentRow.TPIdentity = content.TPIdentity;


			if (isNew)
			{
				Storage.Content.AddContentRow(contentRow);
			}


			if (transaction == null)
			{
				AcceptChanges();
			}
		}

		public IList<CustomerContentEntity> ReadContentByProject(Project project)
		{
			List<CustomerContentEntity> result = new List<CustomerContentEntity>();

			foreach (dtoXmlStorage.ContentRow contentrow in Storage.Content)
			{
				if (string.Compare(contentrow.ProjectId, project.Id, true) == 0)
				{
					CustomerContentEntity content = (CustomerContentEntity)ReadContent(contentrow.Id);
					content.Permissions = contentrow.Permissions;
					content.Project = project;
					result.Add(content);
				}
			}

			return result;
			
		}
		#endregion



		#region IContentDAL Members


		public void SaveContentPermissions(IList<CustomerContentEntity> permissionsToSave)
		{
			foreach (CustomerContentEntity cce in permissionsToSave)
			{
				dtoXmlStorage.ContentRow contentRow = Storage.Content.FindById(cce.Id);

				contentRow.Permissions = cce.Permissions;

			}

			AcceptChanges();
		}

		#endregion
	}
}
